package com.liuheng.spzx.user.utils;

import jakarta.mail.Authenticator;
import jakarta.mail.PasswordAuthentication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

@Configuration
public class MailConfig {


    @Bean
    public JavaMailSender getJavaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

        // 设置邮件发送服务器属性
        mailSender.setHost("smtp.qq.com");
        mailSender.setPort(465);

        // 设置SMTP身份验证信息
        mailSender.setUsername("2100981240@qq.com");
        mailSender.setPassword("pgcjgoqnqyimddji");

        // 设置JavaMail属性以启用SSL/TLS
        Properties javaMailProperties = new Properties();
        javaMailProperties.put("mail.transport.protocol", "smtp");
        javaMailProperties.put("mail.smtp.auth", "true");
        javaMailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        javaMailProperties.put("mail.smtp.socketFactory.fallback", "false");
        javaMailProperties.setProperty("mail.smtp.ssl.enable", "true"); // 对于QQ邮箱465端口，必须设置为true

        // 设置邮件编码为UTF-8（可选，视需求）
        javaMailProperties.setProperty("mail.smtp.charset", "utf-8");
        javaMailProperties.setProperty("mail.mime.charset", "utf-8");

        mailSender.setJavaMailProperties(javaMailProperties);
        return mailSender;
    }


}